package bg.uni_sofia.conf_manager.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@Entity
@NamedQueries({
		@NamedQuery(name = "getAllConferences", query = "SELECT c FROM Conference c"),
		@NamedQuery(name = "getConferenceById", query = "SELECT c FROM Conference c WHERE c.id = :id"),
		@NamedQuery(name = "getConferencesByEmployee", query = "SELECT c FROM Conference c WHERE c.employee.id = :id"),
		@NamedQuery(name = "getConferenceByName", query = "SELECT c FROM Conference c WHERE c.name = :name"),
		})
@XmlRootElement
public class Conference implements Serializable {

	@Override
	public String toString() {
		return "Conference [id=" + id + ", name=" + name + ", location="
				+ location + ", startTime=" + startTime + ", endTime="
				+ endTime + ", employee=" + employee + ", lectures=" + lectures
				+ ", lecturers=" + lecturers + "]";
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = -4715131273327075836L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	private String name;
	private String location;
	@Temporal(TemporalType.DATE)
	private Date startTime;
	@Temporal(TemporalType.DATE)
	private Date endTime;
	@Temporal(TemporalType.TIMESTAMP)
	private Date creationDate;
	@ManyToOne
	private Employee employee;
	@OneToMany
	@XmlInverseReference(mappedBy = "conference")
	private List<Lecture> lectures;
	@ManyToMany
	@XmlInverseReference(mappedBy = "conferences")
	private List<Lecturer> lecturers;

	public Conference() {
	}

	public Conference(String name, String location, Date startTime,
			Date endTime, Employee employee, List<Lecture> lectures, List<Lecturer> lecturers) {
		super();
		this.name = name;
		this.location = location;
		this.startTime = startTime;
		this.endTime = endTime;
		this.employee = employee;
		this.lectures = lectures;
		this.creationDate = new Date();

		this.lecturers = lecturers;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public List<Lecturer> getLecturers() {
		return lecturers;
	}

	public void setLecturers(List<Lecturer> lecturers) {
		this.lecturers = lecturers;
	}
	public void setName(String name) {
		this.name = name;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	@XmlTransient
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public List<Lecture> getLectures() {
		return lectures;
	}

	public void setLectures(List<Lecture> lectures) {
		this.lectures = lectures;
	}


	public void addLecture(Lecture lecture){
		if(lecture!=null){
			getLectures().add(lecture);
		}
	}
	
	public void addLecturer(Lecturer lecturer){
		if(lecturer!=null){
			getLecturers().add(lecturer);
		}
	}

	public Date getCreationDate() {
		return creationDate;
	}

	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return name.hashCode()*31;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null || !(obj instanceof Conference)){
			return false;
		}
		
		if(((Conference) obj).name.equals(name)){
			return true;
		}else{
			return false;
		}
	}
}